package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsStorageMapper;
import com.qf.pojo.DtsStorage;
import com.qf.service.DtsStorageService;
import com.qf.util.AliyunOSSUtil;
import com.qf.util.UUID;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.File;
import java.io.InputStream;
import java.util.Date;

/**
 * @author 邢式瑞
 * @version 1.0
 * @project E-commercebackGroundManagementSystem
 * @date 2022/9/22 11:01
 *      * @param endpoint          OSS对外服务的访问域名
 *      * @param accessKeyId       accessKey账号
 *      * @param accessKeySecret   accessKey密码
 *      * @param bucketName        桶名字
 *      * @param objectName        完整文件名, 例如abc/efg/123.jpg
 *      * @param content           文件内容, byte数组格式
 */
@Service
public class DtsStorageServiceImpl  implements DtsStorageService {

    @Autowired
    private DtsStorageMapper dtsStorageMapper;
    @Value("${aliyun.oss.endpoint}")
    private String endPoint;
    @Value("${aliyun.oss.accessKeyId}")
    private String accessKeyId;
    @Value("${aliyun.oss.accessKeySecret}")
    private String accessKeySecret;
    @Value("${aliyun.oss.bucketName}")
    private String bucketName;
    private  String  objectName;
   private byte[] content;

    @Override
    public DtsStorage create(byte[] bytes, String fileName,long size) throws Exception {
        content=bytes;
        String ext = FilenameUtils.getExtension(fileName);
        objectName= UUID.randomUUID().toString().replaceAll("-","")+"."+ext;

        AliyunOSSUtil aliyunOSSUtil = new AliyunOSSUtil();
        String s = aliyunOSSUtil.uploadImage
                (endPoint,
                        accessKeyId,
                        accessKeySecret,
                        bucketName,
                        objectName,
                        content);
        DtsStorage dtsStorage = new DtsStorage();
     dtsStorage.setKey(objectName);
     dtsStorage.setName(objectName);
     dtsStorage.setType(ext);
     dtsStorage.setSize(size);
      dtsStorage.setUrl(s);
      dtsStorage.setAddTime(new Date());
      dtsStorage.setUpdateTime(new Date());
      dtsStorageMapper.insert(dtsStorage);
        return dtsStorage;
    }

    @Override
    public Page<DtsStorage> findByPage(int page, int limit, String sort, String order) {
        Page<DtsStorage> dtsStoragePage = new Page<>(page, limit);
        QueryWrapper<DtsStorage> dtsStorageQueryWrapper = new QueryWrapper<>();
        dtsStorageQueryWrapper.orderBy(!StringUtils.isEmpty(sort),"asc".equalsIgnoreCase(order),sort).eq("deleted",false);
        return dtsStorageMapper.selectPage(dtsStoragePage,dtsStorageQueryWrapper);
    }

    @Override
    public void deleteById(DtsStorage dtsStorage) {
        dtsStorageMapper.updateById(dtsStorage);
    }
}
